<?php
defined('IN_PHPCMS') or exit('No permission resources.'); 
/**
 * AJAX下拉加载 /api.php?op=ajax_append&modelid=3&catid=1&p=1
 */
$db = '';
$db = pc_base::load_model('content_model');
$MODEL = getcache('model','commons');
$CATEGORYS = getcache('category_content_1','commons');
$modelid = intval($_GET['modelid']);
$pagebase = 20;
$pagenum = 10;
$p = $_GET['p'] ? intval($_GET['p']) : 1;
//v9_picture
$tablename = $db->table_name = $db->db_tablepre.$MODEL[$modelid]['tablename'];
$where = $_GET['catid'] ? "catid = ".intval($_GET['catid']) : 1;
$limit = $pagebase + ($p * $pagenum);
$r = $db->query("SELECT * FROM `$tablename` WHERE $where ORDER BY id DESC LIMIT $limit,$pagenum");
$rs = $db->fetch_array($r);

$total = $db->query("SELECT COUNT(*) AS `count` FROM `$tablename` WHERE $where");
$result = $db->fetch_array($total);
$total = $result[0]['count'] - $pagebase;
$totalpage = round($total/10);
if($totalpage >1 && $p <= 10){
  $data = array();
  $data['code'] = 1;
  $data['msg'] = 'OK';
  if($totalpage > 10){
    $data['pagetotal'] = 10;
  }else{
    $data['pagetotal'] = $totalpage;
  }
  $tablename = $db->table_name = $db->db_tablepre.$MODEL[$modelid]['tablename']."_data";
  foreach ($rs as $key => $value) {
    $r = $db->get_one(array('id'=>$rs[$key]['id']));
    $picnum = count(json_decode($r['pictureurls'], true));
    $inputtime = date("Y-m-d",$rs[$key]['inputtime']);
    $thumb = $rs[$key]['thumb'].'?x-oss-process=style/midpic';
    $catid = $rs[$key]['catid'];
    $enjoynum = isset($rs[$key]['digg']) ? $rs[$key]['digg'] : 0;
    $catname = $CATEGORYS[$catid]['catname'];
    $caturl = $CATEGORYS[$catid]['url'];
    $list = <<<EOF
<div class="layui-box">
  <div class="layui-actions">
    <a class="layui-thumb" href="{$rs[$key]['url']}" target="_blank"><img lay-src="{$thumb}" alt="{$rs[$key]['title']}" /></a>
    <div class="layui-infos">
     <div class="layui-digg"><i class="layui-heart"></i><span class="layui-badge">{$enjoynum}人喜欢</span></div>
     <div class="layui-type"><i class="layui-icon">&#xe7a0;</i><span class="layui-badge"><a href="{$caturl}" target="_blank">{$catname}</a></span></div>
    </div>  
  </div>
  <div class="layui-stats">
    <div class="layui-tips">
      <div class="layui-piccount"><i class="layui-icon">&#xe64a;</i><span class="layui-badge layui-count" data-num="{$picnum}">{$picnum}P</span></div>
      <div class="layui-posttime"><i class="layui-icon">&#xe609;</i><span class="layui-badge layui-date">{$inputtime}</span></div>
    </div>
   	<div class="layui-title">
      <h2><a href="{$rs[$key]['url']}" title="{$rs[$key]['title']}" target="_blank">{$rs[$key]['title']}</a></h2>
    </div>
  </div>
</div>
EOF;
  $data['list'][$key] = htmlspecialchars($list);
  }
}else{
  $data['code'] = 0;
  $data['msg'] = '数据获取失败，请稍后再试！';
}

echo json_encode($data, JSON_FORCE_OBJECT);
?>